/***************************************************************************                            *
 *Author: Achilles Zhang												   *
 *Date: 2012.01.28														   *
 *File Name: ShadowCaster.h											   *
 *Description: The file is used to declare the function of shadow caster	       *
 ***************************************************************************/
#ifndef SHADOWCASTER_H
#define SHADOWCASTER_H

#include "GlobalHead.h"
#include "View3DInventor.h"
#include "MainWindow.h"
#include "Dialog/ShadowCasterDialog.h"
#include "CustomPlayer.h"

#include "CKAll.h"

using namespace Gui;

struct ShadowCasterStruct{
	CKCamera*		m_Camera;
	CKTexture*		m_ShadowTexture;
	CKMaterial*		m_Material;
	CKMaterial*		m_BlackMaterial;
	CKMaterial*		m_ReplacingMaterial;
	int				m_ReceiverAttribute;
};

class ShadowCaster
{
public:
	ShadowCaster();
	~ShadowCaster();

public:
	static ShadowCaster* _singleton;
	static ShadowCaster* Instance();
	static float minLightDistance;
	static float maxLightDistance;
	static float lightRadius;
	static int shadowSize;
	static CKBOOL isSoft;
	static VxVector east;
	static float angle;
	static CK_ID lightID;
	static void destruct();

public:
	void show();
	void initialization();
	static CKBOOL  do_ShadowCaster(/*const CKBehaviorContext& behcontext*/CKRenderContext *Dev,/*CKRenderObject* ro,*/void *Argument);
	static CKBOOL RenderObjectsToTexture(CKContext* ctx,/*CK3dEntity* ent,*/CKRenderContext* rc, ShadowCasterStruct* tss, CK3dEntity* light, float lmin,float lmax,float& minz,CKBOOL softshadow, float *retrievedMaxx, float *retrievedMaxy);
	static void HierarchicalRendering(CK3dEntity* ent,CKRenderContext* dev,ShadowCasterStruct* tss);
	static void DrawFillRectangleGradient(CKRenderContext *dev,VxRect& rect,const VxColor& start, const VxColor& end);
	static CKERROR loadShadowCaster(ShadowCasterStruct* tss, int);
	static void ShadowCasterAttributeCallback(int AttribType,BOOL Set,CKBeObject *obj,void *arg);
	static void setShadowSize(int);
	static CKERROR deleteShadowCaster();

	static ShadowCasterStruct* tss[4];
	static bool isShadering;
	static bool isInit;
	static VxVector barycenter;
	static VxVector minV;
	static VxVector maxV;
	static VxVector lightPos;
	static void caculateBaryCenter();
	static void caculateEntBox();
	static void setLightPos();
	static void setAngle(int);

	static bool eventCallback(CKRenderContext *context,void *Argument);
private:
	ShadowCasterDialog* dialog;
};

#endif //SHADOWCASTER_H